﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;

namespace DemoGame1
{
    public class CharacterStatusBar : VisibleGameEntity
    {
        List<MyButton> myButtons;
        private static float MaxHealth = 1000;
        private static float MaxMana = 100;
        private float _Health = MaxHealth;
        private float _Mana = MaxMana;

        public float Health
        {
            get { return _Health; }
            set
            {
                if (0 <= value && value <= MaxHealth)
                    _Health = value;
            }
        }

        public float Mana
        {
            get { return _Mana; }
            set
            {
                if (0 <= value && value <= MaxMana)
                    _Mana = value;
            }
        }

        public CharacterStatusBar(ContentManager content, string charName)
        {
            myButtons = new List<MyButton>();
            XmlDocument doc = new XmlDocument();
            doc.Load(XMLDataProvider.strCharacterStatusBar);
            XmlElement backGround = (XmlElement)doc.SelectSingleNode("//BackGround");
            //add char
            XmlElement buttons = (XmlElement)doc.SelectSingleNode("//Buttons");
            foreach (XmlNode button in buttons.ChildNodes)
            {
                MyButton bt = new MyButton(content, (XmlElement)button);
                myButtons.Add(bt);
            }
            if (charName == "Ken")
            {
                myButtons.RemoveAt(2);
            }
            if (charName == "Ryu")
            {
                myButtons.RemoveAt(1);
            }
        }
        Vector2 HP = Vector2.Zero;
        Vector2 MN = Vector2.Zero;

        public override void Update(GameTime gameTime)
        {
            {
                float x = myButtons[2].Size.X;
                float y = myButtons[2].Size.Y;
                x = _Health / MaxHealth * x;
                HP = new Vector2(x, y);
            }
            {
                float x = myButtons[3].Size.X;
                float y = myButtons[3].Size.Y;
                x = _Mana / MaxMana * x;
                MN = new Vector2(x, y);
            }
        }

        public override void Draw(SpriteBatch sb, GameTime gameTime)
        {
            for (int i = 0; i < myButtons.Count; i++)
            {
                switch (i)
                {
                    case 2:
                        myButtons[i].DrawwithDepth(sb, gameTime, myButtons[i].TopLeft, HP,0f);
                        break;

                    case 3:
                        myButtons[i].DrawwithDepth(sb, gameTime, myButtons[i].TopLeft, MN, 0f);
                        break;
                    default:
                        myButtons[i].Draw(sb, gameTime);
                        break;
                }
            }
        }
    }
}
